package org.marsdonne.auth.restful;

import org.marsdonne.archives.Client;
import org.marsdonne.auth.wechat.QrCodeRequest;
import org.marsdonne.auth.wechat.WechatAuthApi;
import org.marsdonne.commons.wechat.AccessTokenServ;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class QrCodeController {

    @Value("${auth.QrCode.wechat.envVersion:}")
    private String envVersion;

    @Resource
    private AccessTokenServ accessTokenServ;

    @Resource
    private WechatAuthApi wechatAuthApi;

    @GetMapping("/qrcode/joinByWechat")
    byte[] joinByWechat(@RequestParam Client client, @RequestParam String page, @RequestParam String scene) {
        String token = this.accessTokenServ.getToken(client).get(AccessTokenServ.Resp4Token.AccessToken.value).toString();
        QrCodeRequest qrCodeRequest = new QrCodeRequest(token, scene);
        qrCodeRequest.setPage(page);
        if (envVersion != null && envVersion.length() > 0)
            qrCodeRequest.setEnvVersion(envVersion);
        return this.wechatAuthApi.getQrCode(qrCodeRequest);
    }
}
